package com.bugbuff.dubbo.server.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bugbuff.common.mapper.UserInfoMapper;
import com.bugbuff.common.pojo.User;
import com.bugbuff.common.pojo.UserInfo;
import com.bugbuff.common.utils.UserThreadLocal;
import com.bugbuff.dubbo.server.pojo.RecommendUser;
import com.bugbuff.common.pojo.PageResult;
import com.bugbuff.dubbo.server.vo.RecommendUserQueryParam;
import com.bugbuff.dubbo.server.vo.TodayBest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TodayBestService {

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserService userService;

    /**
     * 获取今日佳人
     */
    public TodayBest getTodayBest() {

        //1、校验token
        User user = UserThreadLocal.get();

        //2、获取数据
        //获取mongodb数据 缘分值
//        System.out.println(recommendApi);
        RecommendUser recommendUser = recommendUserService.getRecommendUser(user.getId());
        //获取userId
        Long userId = recommendUser.getUserId();
        //根据userId查询对应的用户信息 userInfo

        /*LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInfo::getId, userId);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);*/

        UserInfo userInfo = userInfoMapper.selectById(userId);
        //3、组装响应前端的vo TodayBest
        TodayBest todayBest = new TodayBest();
        todayBest.setId(userInfo.getId());
        todayBest.setAge(userInfo.getAge());
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setFateValue(recommendUser.getScore().longValue());
        todayBest.setTags(userInfo.getTags().split(","));

        return todayBest;
    }

    /**
     * 推荐列表查询
     */
    public PageResult queryRecommendation(RecommendUserQueryParam param) {
//        解析token
        User user = UserThreadLocal.get();

        PageResult pageResult = new PageResult();
        pageResult.setPage(param.getPage());
        pageResult.setPagesize(param.getPagesize());

//        对应用户的推荐人（一堆）
        List<RecommendUser> recommendUserList = recommendUserService.queryList(user.getId(), param.getPage(), param.getPagesize());
        List<TodayBest> list = new ArrayList<>();
//        遍历recommendUserList
        for (RecommendUser recommendUser : recommendUserList) {
            //获取userId
            Long userId = recommendUser.getUserId();

//            根据userId查询对应的用户信息userinfo
            LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
//            条件控制
            wrapper.eq(UserInfo::getId, userId);
//            动态条件的判断
            /*if (StringUtils.isNotBlank(param.getGender())) {
                wrapper.eq(UserInfo::getSex, "man".equals(param.getGender()) ? 1 : 2);
            }
            if (param.getAge() != null) {
                wrapper.le(UserInfo::getAge, param.getAge());
            }*/

            UserInfo userInfo = userInfoMapper.selectOne(wrapper);
            if (userInfo != null) {
                //3、组装响应前端的vo TodayBest
                TodayBest todayBest = new TodayBest();
                todayBest.setId(userInfo.getId());
                todayBest.setAge(userInfo.getAge());
                todayBest.setAvatar(userInfo.getLogo());
                todayBest.setNickname(userInfo.getNickName());
                todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
                todayBest.setFateValue(recommendUser.getScore().longValue());
                todayBest.setTags(userInfo.getTags().split(","));
                list.add(todayBest);
            }

        }
        pageResult.setItems(list);
        return pageResult;
    }
}
